//
//  TSCVCForgetPasswd.m
//  Tsc_IOS
//
//  Created by Rio on 15-5-15.
//  Copyright (c) 2015年 idream. All rights reserved.
//

#import "TSCVCForgetPasswd.h"
#import "TSCSDK.h"
#import "TSCVCForget2EmailNotExist.h"
#import "TSCVCForget3ResendVeriEmail.h"
#import "TSCVCForget4ResendVeriEmailSuccessful.h"
#import "TSCVCForget5ResendVeriEmailNotReceived.h"
#import "TSCVCForget6ResetPasswdVeriEmailSended.h"

@implementation TSCVCForgetPasswd

+ (id) vcForgetPasswdWithStoryboard {
    UIStoryboard * sb = [UIStoryboard storyboardWithName:UI_STORYBOARD_NAME_MAIN bundle:[NSBundle mainBundle]];
    return [sb instantiateViewControllerWithIdentifier:UI_VC_NAME_FORGET_PASSWD];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 设置自定义导航栏
    _nbc = [[TSCMyNaviBarCreator alloc]initAndCreateWithViewController:self hasGoBackButton:YES goBackVC:nil hasAppLogo:YES titleResourceKey:@"title_input_reg_email" iconResourceNameArray:nil iconClickedListenerArray:nil];
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

#pragma mark 私有方法


/* private */ - (void) sendResetPasswdEmail_S1:(NSString *)emailAddress {
    // 拼装请求
    NSMutableDictionary * reqDic = [[NSMutableDictionary alloc] init];
    [reqDic setObject:emailAddress forKey:@"email"];
    
    // 发送请求
    [TSCMyHttpUtil postWithUrl:URL_FORGET_PASSWD_SEND_RESET_PASSWD_EMAIL dictionary:reqDic completionHandler:^(ServerRespType serverRespType, NSString * respString, NSDictionary *respDictionary, NSInteger code, NSString *msg, NSDictionary *contentDictionary) {
        // 服务器返回错误处理
        switch (serverRespType) {
            case ServerRespTypeSuccess:
                break;
            case ServerRespTypeErrNil:
            case ServerRespTypeErrJson: {
                [self dissmisDialog];
                [TSCMyUIUtil popErrWithResourceKey:@"err_web_server_unavailable"];
                return;
            }
            case ServerRespTypeErrCode:{
                [self dissmisDialog];
                [TSCMyUIUtil popErrWithString:msg];
                return;
            }
            default: {
                [self dissmisDialog];
                [TSCMyUIUtil popErrWithResourceKey:@"err_web_server_unavailable"];
                return;
            }
                
        }
        
        // 解析服务器返回
        BOOL isEmailExist = [[contentDictionary objectForKey:@"is_email_exist"] boolValue];
        BOOL isEmailVerified = [[contentDictionary objectForKey:@"is_email_verified"] boolValue];
        
        // 继续后续逻辑...
        [self sendResetPasswdEmail_S2:isEmailExist isEmailVerified:isEmailVerified];
    }];
    
}

- (void) sendResetPasswdEmail_S2:(BOOL)isEmailExist isEmailVerified:(BOOL)isEmailVerified {
    // 关闭提示dialog
    [self dissmisDialog];
    
    // 可能的情况：
    // 1. 邮箱不存在
    // 2. 邮箱存在，但未验证。需要用户从app端点击“发送验证邮件”，才能使server发送邮箱验证邮件
    // 3. 邮箱存在，且已验证。server已自动发送了重置密码验证邮件
    
    // 邮箱不存在
    if (isEmailExist == NO) {
        // 提示用户邮箱还不存在
        // TODO 原来rsn的做法是，依然提示用户验证邮件已发送。这样做的好处是不会让用户试出来系统里已经有哪些邮箱了
        
        TSCVCForget2EmailNotExist * vc = [TSCVCForget2EmailNotExist vcForget2EmailNotExistWithStoryboard];
        [[self navigationController] pushViewController:vc animated:YES];
        
        // VC间传递参数
        NSDictionary * inputDic = [NSDictionary dictionaryWithObject:[TSCMyUtil trim:_tfEmail.text] forKey:AEKI_EMAIL];
        [[NSNotificationCenter defaultCenter] addObserver:vc selector:@selector(SELECTOR_GET_VC_INPUT) name:UI_VC_NAME_FORGET_2_EMAIL_NOT_EXIST object:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:UI_VC_NAME_FORGET_2_EMAIL_NOT_EXIST object:inputDic];
        
        return;
    }
    
    // 邮箱未验证
    if (isEmailVerified == NO) {
        TSCVCForget3ResendVeriEmail * vc = [TSCVCForget3ResendVeriEmail vcForget3ResendVeriEmailWithStoryboard];
        [[self navigationController] pushViewController:vc animated:YES];
        
        // VC间传递参数
        NSDictionary * inputDic = [NSDictionary dictionaryWithObject:[TSCMyUtil trim:_tfEmail.text] forKey:AEKI_EMAIL];
        [[NSNotificationCenter defaultCenter] addObserver:vc selector:@selector(SELECTOR_GET_VC_INPUT) name:UI_VC_NAME_FORGET_2_EMAIL_NOT_EXIST object:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:UI_VC_NAME_FORGET_2_EMAIL_NOT_EXIST object:inputDic];
        
        return;
    }
    
    // 邮箱已验证
    TSCVCForget6ResetPasswdVeriEmailSended * vc = [TSCVCForget6ResetPasswdVeriEmailSended vcForget6ResetPasswdVeriEmailSendedWithStoryboard];
    [[self navigationController] pushViewController:vc animated:YES];
    
    // VC间传递参数
    NSDictionary * inputDic = [NSDictionary dictionaryWithObject:[TSCMyUtil trim:_tfEmail.text] forKey:AEKI_EMAIL];
    [[NSNotificationCenter defaultCenter] addObserver:vc selector:@selector(SELECTOR_GET_VC_INPUT) name:UI_VC_NAME_FORGET_6_RESET_PASSWD_VERI_EMAIL_SENDED object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:UI_VC_NAME_FORGET_6_RESET_PASSWD_VERI_EMAIL_SENDED object:inputDic];
}

#pragma mark delegate

/**
 * return键收回键盘
 *
 * @date 2015-05-15
 * @author Rio
 */
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

/**
 * 点击空白区域收回键盘
 *
 * @date 2015-05-15
 * @author Rio
 */
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [_tfEmail resignFirstResponder];
}

- (IBAction)btnNextClicked:(id)sender {
    // 收回“可能”打开的键盘
    [_tfEmail resignFirstResponder];
    
    NSString * emailAddress = [TSCMyUtil trim:_tfEmail.text];
    
    if ([TSCMyCheckUtil checkWithString:emailAddress regexType:RegexTypeEmail] == NO) {
        [TSCMyUIUtil popErrWithResourceKey:@"err_format_email"];
        return;
    }
    
    // 提示用户正在发送
    [self showDialogWithResourceKey:@"content_sending" isProcessing:YES];
    
    // （尝试）发送重置密码验证邮件
    [self sendResetPasswdEmail_S1:emailAddress];
}



@end
